package wpmp.auth.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wcommons.httputils.ResponseUtils;
import wcommons.httputils.quickbean.RespInfo;
import wpmp.auth.service.CodeService;
import wpmp.utils.quickbean.Code;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:59:34 PM Mar 25, 2013
 */
@Controller
@RequestMapping("/code.do")
public class CodeController {

	private static final Logger LOG = LoggerFactory.getLogger(CodeController.class);

	private CodeService service;

	@Autowired
	public void setService(CodeService service) {
		this.service = service;
	}

	@RequestMapping(params = "action=list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "sortType", required = true) String sortType) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to getCodeList by [{}]", req.getRemoteAddr(), sortType);
		}

		final List<Code> results = service.queryForList(sortType);

		ResponseUtils.renderJson(resp, new RespInfo<List<Code>>(results).toString());
	}

	@RequestMapping(params = "action=get", method = { RequestMethod.GET, RequestMethod.POST })
	public void get(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "sortType", required = true) String sortType,
			@RequestParam(value = "value", required = true) String value) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to getCode by [{}, {}]", req.getRemoteAddr(), sortType, value);
		}

		final Code result = service.queryForObject(sortType, value);

		ResponseUtils.renderJson(resp, new RespInfo<Code>(result).toString());

	}
}
